package com.mathquiz;

import java.io.*;
import java.util.List;

public class Utils {
    /* 按 cols 列输出 CSV，末尾自动换行 */
    public static void saveCSV(List<Question> list, String path, int cols) throws IOException {
        new File(path).getParentFile().mkdirs();
        try (PrintWriter pw = new PrintWriter(new FileWriter(path))) {
            for (int i = 0; i < list.size(); i++) {
                pw.print(list.get(i).expression + "=" + list.get(i).answer);
                if ((i + 1) % cols == 0) pw.println();
                else pw.print(",");
            }
            if (list.size() % cols != 0) pw.println();
        }
    }
}